package com.huatai.bi.utils.FilterComponent;

import com.alibaba.fastjson2.JSONArray;
import com.huatai.bi.constant.SystemConstant;
import com.huatai.common.error.exception.ServiceException;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Data
public class FieldColumn {
	@ApiModelProperty(value = "字段中文名称")
	String label;

	@ApiModelProperty(value = "字段名称")
	String fieldName;

	@ApiModelProperty(value = "条件")
	String filterCondition;

	@ApiModelProperty(value = "值")
	Object value1;

	@ApiModelProperty(value = "值")
	Object value2;

	public FieldColumn(){}
	public FieldColumn(String label,String fieldName, String filterCondition, String value1){
		this.label = label;
		this.fieldName = fieldName;
		this.filterCondition = filterCondition;
		this.value1 = value1;
	}
	public FieldColumn(String label,String fieldName, String filterCondition, String value1, String value2){
		this.label = label;
		this.fieldName = fieldName;
		this.filterCondition = filterCondition;
		this.value1 = value1;
		this.value2 = value2;
	}

	public String getLabel(){
		return null != label? label : fieldName;
	}

	public String getExp(){
		if(StringUtils.isBlank(filterCondition)){
			throw new ServiceException(String.format("参数 %s 对应条件为空",label));
		}
		//非空时校验值是否为空
		if(!StringUtils.equalsAny(filterCondition,SystemConstant.FILTER_CONDITION_NULL,SystemConstant.FILTER_CONDITION_NONULL)){
			if(ObjectUtils.isEmpty(value1) || "".equals(value1)){
				throw new ServiceException(String.format("参数 %s 对应值为空",label));
			}
			if(SystemConstant.FILTER_CONDITION_BA.equals(filterCondition)){
				if(ObjectUtils.isEmpty(value2) || "".equals(value2)){
					throw new ServiceException(String.format("参数 %s 对应第二个值为空",label));
				}
			}
		}
		String fieldNameStr = "`"+fieldName+"`";

		switch (filterCondition){
			case SystemConstant.FILTER_CONDITION_EQ:
				return fieldNameStr+" = '"+value1+"'";
			case SystemConstant.FILTER_CONDITION_NE:
				return fieldNameStr+" != '"+value1+"'";
			case SystemConstant.FILTER_CONDITION_IN:
				List<String> values =(List)value1;
				StringBuffer inBuffer = new StringBuffer(fieldNameStr).append(" in (");
				for (int i = 0; i < values.size(); i++) {
					if(i > 0){
						inBuffer.append(",");
					}
					String v = values.get(i);
					if("空".equals(v)){
						v = "";
					}
					inBuffer.append("'").append(v).append("'");
				}
				inBuffer.append(" )");
				if(values.contains("空")){
					inBuffer.append(" or ").append(fieldNameStr).append(" is null");
				}
				return inBuffer.toString();
			case SystemConstant.FILTER_CONDITION_NOIN:
				List<String> notValues =(List)value1;
				StringBuffer notInBuffer = new StringBuffer(fieldNameStr).append(" not in (");
				for (int i = 0; i < notValues.size(); i++) {
					if(i > 0){
						notInBuffer.append(",");
					}
					String v = notValues.get(i);
					if("空".equals(v)){
						v = "";
					}
					notInBuffer.append("'").append(notValues.get(i)).append("'");
				}
				notInBuffer.append(" )");
				if(notValues.contains("空")){
					notInBuffer.append(" and ").append(fieldNameStr).append(" is not null");
				}
				return notInBuffer.toString();
			case SystemConstant.FILTER_CONDITION_LIKE:
				return fieldNameStr+" like '%"+value1+"%'";
			case SystemConstant.FILTER_CONDITION_NOLIKE:
				return fieldNameStr+" not like '%"+value1+"%'";
			case SystemConstant.FILTER_CONDITION_NULL:
				return fieldNameStr+" is null";
			case SystemConstant.FILTER_CONDITION_NONULL:
				return fieldNameStr+" is not null";
			case SystemConstant.FILTER_CONDITION_GT:
				return fieldNameStr+" > '"+value1+"'";
			case SystemConstant.FILTER_CONDITION_GE:
				return fieldNameStr+" >= '"+value1+"'";
			case SystemConstant.FILTER_CONDITION_LT:
				return fieldNameStr+" < '"+value1+"'";
			case SystemConstant.FILTER_CONDITION_LE:
				return fieldNameStr+" <= '"+value1+"'";
			case SystemConstant.FILTER_CONDITION_BA:
				return "( "+fieldNameStr+" >= '"+value1+"' and "+fieldNameStr+" <= '"+value2+"' )";
			default:throw new ServiceException(String.format("暂不支持 %s 的表达式",filterCondition));
		}
	}
}
